package org.cid54.bukkit.command;

import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.event.player.PlayerChatEvent;
import org.cid54.bukkit.Cid54PlayerListener;
import org.cid54.bukkit.command.util.PlayerCommand;
import org.cid54.bukkit.model.BluePrint;
import org.cid54.bukkit.model.BluePrintBlock;
import org.cid54.bukkit.model.BluePrintDao;

/**
 *
 * @author fgo
 */
public class ExportBluePrintCommand implements PlayerCommand {

    @Override
    public boolean execute(String command, PlayerChatEvent event, Cid54PlayerListener listener) {

        String[] commands = command.toLowerCase().split(" ");

        if (commands.length > 4) {

            int xW = new Integer(commands[1]);
            int zW = new Integer(commands[2]);
            int yW = new Integer(commands[3]);
            String fileName = commands[4];

            BluePrint bluePrint = new BluePrint();
            bluePrint.setName(fileName);

            Location loc = Cid54PlayerListener.cloneLocation(event.getPlayer().getLocation());
            World w = event.getPlayer().getWorld();

            for (int y = -yW / 2; y < yW / 2; y++) {

                for (int x = -xW / 2; x < xW / 2; x++) {

                    for (int z = -zW / 2; z < zW / 2; z++) {

                        Location l = Cid54PlayerListener.cloneLocation(loc);

                        l.setY(l.getY() + y);
                        l.setX(l.getX() + x);
                        l.setZ(l.getZ() + z);

                        BluePrintBlock block = new BluePrintBlock();

                        block.setX(x);
                        block.setY(y);
                        block.setZ(z);
                        block.setMatId(w.getBlockTypeIdAt(l));
                        
                        bluePrint.getBlocks().add(block);

                    }

                }

            }

            BluePrintDao.instance.exportBluePrint(bluePrint);

            event.getPlayer().sendMessage("Export done.");

            return true;
        } else {
            return false;
        }

    }

    @Override
    public String getCommand() {
        return "export";
    }

    @Override
    public String getHelpText() {
        return "Export <xWidht> <zWidht> <height> <fileName>";
    }
}
